package com.sxl.main;

import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.PrimitiveSink;

public class BloomFilterDemo {

    public static void main(String[] args) {
        BloomFilter<String> bloomFilter = getBloomFilter();
        boolean b = bloomFilter.mightContain("1");
        bloomFilter.put("1");
        b = bloomFilter.mightContain("1");
        System.out.println(b);
    }

    private static BloomFilter<String> getBloomFilter() {
        final BloomFilter<String> bloomFilter = BloomFilter.create(new Funnel<String>() {

            private static final long serialVersionUID = 1L;

            @Override
            public void funnel(String args0, PrimitiveSink primitiveSink) {
                primitiveSink.putString(args0, Charsets.UTF_8);
            }
        }, 1024*1024*26, 0.0000001d);
        return bloomFilter;
    }

    private static boolean containsAsin(String asin, final BloomFilter<String> bloomFilter){
        if (asin== null || "".equals(asin)){
            return true;
        }
        boolean exists = bloomFilter.mightContain(asin);
        if (!exists){
            bloomFilter.put(asin);
        }
        return exists;
    }
}
